package homework2.NearestPoints;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Toolkit;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

/**
 * MainFrame
 * 
 * @author eDisOn 2009212634 lilh09@mails.tsinghua.edu.cn
 * 
 */
public class MainFrame extends JFrame
{
	private boolean FIRST_TIME = true;
	private int ManuFrameIndex = 0;
	private int AutoFrameIndex = 0;

	private static Dimension defaultSize;
	private static java.awt.Point defaultLocation;

	static
	{
		int x = Toolkit.getDefaultToolkit().getScreenSize().width;
		int y = Toolkit.getDefaultToolkit().getScreenSize().height;

		// Set default size as the 4/5 of screen size
		defaultSize = new Dimension((x * 5) / 6, (y * 5) / 6);

		// Set the default location in the center of screen
		defaultLocation = new java.awt.Point(x / 2 - defaultSize.width / 2, y
				/ 2 - defaultSize.height / 2);
	}

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = -7020854943642829869L;

	// Used to contain JInteralFrame
	private JDesktopPane destopPane = null;

	public MainFrame()
	{
		super(
				"Nearest Points Problem  SN:2009212634  Email:lilh09@mails.tsinghua.edu.cn");
		init();
	}

	private void init()
	{
		destopPane = new JDesktopPane();

		/* Set MenuBar */
		JMenu menu = new JMenu("Add");

		JMenuItem demoSingleMenuItem = new JMenuItem(
				"Demo For several pairs of nearest points");
		demoSingleMenuItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				SeveralPairsDemoFrame demoFrame = new SeveralPairsDemoFrame();
				destopPane.add(demoFrame);
				demoFrame.toFront();
			}
		});
		menu.add(demoSingleMenuItem);

		JMenuItem manuMenuItem = new JMenuItem("Manual specified nearest points problem");
		manuMenuItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				ManuNearestPointsFrame manuNPFrame = new ManuNearestPointsFrame(
						ManuFrameIndex++);
				if (FIRST_TIME)
				{
					JOptionPane
							.showMessageDialog(manuNPFrame,
									"Please click the free room to create points!");
					FIRST_TIME = false;
				}

				// register this JInteralFrame into MainFrame
				destopPane.add(manuNPFrame);
				manuNPFrame.toFront();
			}
		});
		menu.add(manuMenuItem);

		JMenuItem autoMenuItem = new JMenuItem("Random-generated nearest points problem");
		autoMenuItem.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				AutoNearestPointsFrame autoNPFrame = new AutoNearestPointsFrame(
						AutoFrameIndex++);

				// register this JInteralFrame into MainFrame
				destopPane.add(autoNPFrame);
				autoNPFrame.toFront();
			}
		});
		menu.add(autoMenuItem);

		JMenuBar menuBar = new JMenuBar();
		menuBar.add(menu);

		// register this JMenuBar into MainFrame
		setJMenuBar(menuBar);

		/* Set DestoPane */
		add(destopPane);

		/* Set frame location, size, closeOperation and visible */
		setSize(defaultSize);
		setLocation(defaultLocation);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	public static void main(String args[])
	{
		MainFrame frame = new MainFrame();
	}
}
